package notes.java.concurrent;

import java.util.concurrent.CountDownLatch;

/**
 * 可以看出多个线程在访问同一个变量的时候出现的异常，线程间的数据没有隔离
 * 输出结果有重复的数据，线程之前没有隔离
 * @author wangcs
 *
 */
public class U16_TestThreadLocal1 {
	
	// 变量str
	private String str;
	
	// get
	private String getStr() {
		return str;
	}
	// set
	public void setStr(String str) {
		this.str = str;
	}

	public static void main(String[] args) {
		// 10个线程
		int threads = 10;
		U16_TestThreadLocal1 t = new U16_TestThreadLocal1();
		
		// 计数器
		CountDownLatch latch = new CountDownLatch(threads);
		for(int i=0; i<threads; i++) {
			Thread thread = new Thread(()->{
				t.setStr("线程名：" + Thread.currentThread().getName());
				System.out.println(t.getStr());
				latch.countDown();
				
			}, "第 - " + i + "个");
			
			thread.start();
		}
		
	}
	

}
